import userApi from '@/api/user/index';
import userWallet from './userWallet';

export default {
  namespaced: true,

  state: {
    user: {},
    p_user: {},
    user_wallet: {}
  },

  getters: {},

  mutations: {
    setUser: (state, data) => {
      state.user = data;
    },
    setPUser: (state, data) => {
      state.p_user = data;
    },
    setUserWallet: (state, data) => {
      state.user_wallet = data;
    }
  },

  actions: {
    ...userWallet.actions,

    findFirstByTokenIsEmpty: (context) => {
      if(JSON.stringify(context.state.user) === '{}') {
        return userApi.findFirstByToken().then((data) => {
          let code = parseInt(data.code);
          if(code !== 0) { return {code:code,msg:data.msg}; }

          data.user.star_level = parseInt(data.user.star_level);
          context.commit('setUser', data.user);
          context.commit('setPUser', data.p_user);
          context.commit('setUserWallet', data.user_wallet);

          return {code: code};
        });
      }

      return {code: 0};
    }
  }
};
